package com.example.basic;

import javax.naming.Name;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangs
 * @version v1.0
 * @decription 线程状态
 * @date 2021/3/28 12:31
 **/
public class ThreadState {
    /**
     * 打印出线程状态
     *
     * @param thread 线程
     * @param time   时间
     */
    static void print(Thread thread, String time) {
        StringBuilder sb = new StringBuilder();
        sb.append(time)
                .append("\n\tThread Name:")
                .append(thread.getName())
                .append("\n\tThread State:")
                .append(thread.getState());
        System.out.println(sb.toString());
    }

    public static void main(String[] args) throws InterruptedException {
        // 状态切换：新建 -> 运行 -> 终止
        Runnable runnable = () -> {
            print(Thread.currentThread(), "运行时：");
        };
        Thread thread = new Thread(runnable, "测试线程00");

        // 刚创建时的线程状态
        print(thread, "未调用start方法：");

        // 线程运行时的状态
        thread.start();

        // 线程结束后的状态
        TimeUnit.SECONDS.sleep(2); //主线程休眠2秒
        print(thread, "线程结束后的状态：");
    }
}
